import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterdemo/utils/toast_util.dart';

class DialogWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("dialog"),
      ),
      body: Scrollbar(
          child: SingleChildScrollView(
        padding: EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            OutlineButton(
              child: Text("simple dialog"),
              onPressed: () {
                _showSimpleDialog(context);
              },
            ),
            OutlineButton(
              child: Text(" dialog"),
              //拿到弹窗的返回值
              onPressed: () async {
                bool delete = await _showAlertDialog(context);
                ToastUtil.showToastCenter("dialog 点击结果=$delete");
              },
            ),
            OutlineButton(
              child: Text("自定义 alert dialog"),
              //拿到弹窗的返回值
              onPressed: () async {
                int index = await _showCustomAlertDialog(context);
                ToastUtil.showToastCenter("dialog 点击结果=$index");
              },
            ),
            OutlineButton(
              child: Text("自定义列表dialog"),
              onPressed: () async {
                int index = await _showListDialog(context);
                ToastUtil.showToastCenter("选择 $index");
              },
            ),
            OutlineButton(
              child: Text("自定义custom dialog"),
              onPressed: () async {
                bool delete = await _showCustomDialog(context);
                ToastUtil.showToastCenter("选择 $delete");
              },
            ),
            OutlineButton(
              child: Text("showModalBottomSheet dialog"),
              onPressed: () async {
                int index = await _showModalBottomSheet(context);
                ToastUtil.showToastCenter("选择 $index");
              },
            ),
            OutlineButton(
              child: Text("showBottomSheet dialog TODO"),
              onPressed: () {
                _showBottomSheetDialog(context);
              },
            ),
            OutlineButton(
              child: Text("Loading Dialog"),
              onPressed: () {
                _showLoadingDialog(context);
              },
            ),
            OutlineButton(
              child: Text("自定义 Loading Dialog"),
              onPressed: () {
                _showCustomLoadingDialog(context);
              },
            ),
            OutlineButton(
              child: Text("日历"),
              onPressed: () {
                _showDatePicker(context);
              },
            ),
          ],
        ),
      )),
    );
  }

  Future<int> _showSimpleDialog(BuildContext context) async {
    int i = await showDialog(
        context: context,
        builder: (BuildContext buildContext) {
          return SimpleDialog(
            title: Text("title"),
            children: <Widget>[
              SimpleDialogOption(
                child: Text("选项1"),
                onPressed: () {
                  Navigator.pop(context, 1);
                },
              ),
              SimpleDialogOption(
                child: Text("选项2"),
                onPressed: () {
                  Navigator.of(context).pop(2);
                },
              ),
            ],
          );
        });
    ToastUtil.showToastCenter("选择了$i");
    return i;
  }

  Future<bool> _showAlertDialog(BuildContext context) {
    return showDialog(
        context: context,
        builder: (BuildContext buildContext) {
          return AlertDialog(
            title: Text(
              "请选择",
              textAlign: TextAlign.center,
            ),
            content: Text("确认删除?"),
            actions: <Widget>[
              Container(
                width: double.maxFinite,
                color: Colors.yellowAccent,
                child: FlatButton(
                  child: Text("确认"),
                  onPressed: () {
                    //返回值
                    Navigator.of(context).pop(true);
                  },
                ),
              ),
//              FlatButton(
//                child: Text("取消"),
//                onPressed: () {
//                  Navigator.of(context).pop(false);
//                },
//              ),
            ],
          );
        });
  }

  Future _showCustomAlertDialog(BuildContext context) async {
    await showDialog(
      context: context,
      builder: (context) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(5))
          ),
          child: Container(
            height: 170,
            child: Column(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.all(10),
                  child: Text(
                    '标题',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      fontSize: 20,
                      color: Colors.black,
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Container(
                    padding: EdgeInsets.all(10),
                    child: Text(
                      '一旦解绑，则解除设备和绑定位置的关联关系',
                      textAlign: TextAlign.center,
                    ),
                  ),
                ),
                Divider(
                  height: 1,
                  color: Colors.grey,
                ),
                Row(
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: Container(
                        height: 50,
                        child: FlatButton(
                          onPressed: () {
                            Navigator.of(context).pop(1);
                          },
                          child: Text('YES'),
                        ),
                      ),
                    ),
                    SizedBox(
                      width: 1,
                      height: 50,
                      child: DecoratedBox(
                        decoration: BoxDecoration(color: Colors.black38),
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child: Container(
                        height: 50,
                        child: FlatButton(
                          onPressed: () {
                            Navigator.of(context).pop(1);
                          },
                          child: Text('NO'),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Future<int> _showListDialog(BuildContext context) async {
    int index = await showDialog(
        context: context,
        builder: (BuildContext buildContext) {
          return Dialog(
            child: Column(
              children: <Widget>[
                ListTile(
                  title: Text("请选择"),
                ),
                Expanded(
                  child: ListView.separated(
                      itemCount: 6,
                      separatorBuilder: (buildContext, position) {
                        if (position % 2 == 0) {
                          return Divider(color: Colors.green);
                        } else {
                          return Divider(color: Colors.blue);
                        }
                      },
                      itemBuilder: (buildContext, position) {
                        return ListTile(
                          title: Text("text $position"),
                          onTap: () {
                            Navigator.of(context).pop(position);
                          },
                        );
                      }),
                ),
                Flex(
                  direction: Axis.horizontal,
                  children: <Widget>[
                    Expanded(
                      child: FlatButton(
                        child: Text("确定"),
                        onPressed: () {
                          Navigator.of(context).pop(0);
                        },
                      ),
                      flex: 1,
                    ),
                    Expanded(
                      child: FlatButton(
                        child: Text("取消"),
                        onPressed: () {
                          Navigator.of(context).pop();
                        },
                      ),
                      flex: 1,
                    ),
                  ],
                ),
              ],
            ),
          );
        });
    return index;
  }

  Future<bool> _showCustomDialog(BuildContext context) {
    return showDialog(
        context: context,
        builder: (BuildContext buildContext) {
          return AlertDialog(
            title: Text("请选择"),
            content: Text("确认删除?"),
            actions: <Widget>[
              FlatButton(
                child: Text("确认"),
                onPressed: () {
                  //返回值
                  Navigator.of(context).pop(true);
                },
              ),
              FlatButton(
                child: Text("取消"),
                onPressed: () {
                  Navigator.of(context).pop(false);
                },
              ),
            ],
          );
        });
  }

  Future<int> _showModalBottomSheet(BuildContext context) {
    return showModalBottomSheet(
        context: context,
        builder: (BuildContext buildContext) {
          return Column(
            mainAxisAlignment: MainAxisAlignment.start,
            //自适应高度
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              FlatButton(
                child: Text("Android"),
                onPressed: () {
                  Navigator.of(context).pop(0);
                },
              ),
              FlatButton(
                child: Text("IOS"),
                onPressed: () {
                  Navigator.of(context).pop(1);
                },
              ),
            ],
          );
        });
  }

  PersistentBottomSheetController<int> _showBottomSheetDialog(
      BuildContext context) {
    return showBottomSheet<int>(
        context: context,
        builder: (BuildContext buildContext) {
          return ListView.builder(
              itemCount: 20,
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text("text $index"),
                  onTap: () {
                    Navigator.of(context).pop(index);
                  },
                );
              });
        });
  }

  void _showLoadingDialog(BuildContext context) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) {
          return AlertDialog(
            content: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                CircularProgressIndicator(),
                Padding(
                  padding: const EdgeInsets.only(top: 26.0),
                  child: Text("正在加载，请稍后..."),
                ),
              ],
            ),
          );
        });
  }

  void _showCustomLoadingDialog(BuildContext context) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) {
          return UnconstrainedBox(
            constrainedAxis: Axis.vertical,
            child: SizedBox(
              width: 280,
              child: AlertDialog(
                content: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    CircularProgressIndicator(),
                    Padding(
                      padding: const EdgeInsets.only(top: 26.0),
                      child: Text("正在加载，请稍后..."),
                    )
                  ],
                ),
              ),
            ),
          );
        });
  }

  Future<DateTime> _showDatePicker(BuildContext context) {
    var date = DateTime.now();
    return showDatePicker(
      context: context,
      initialDate: date,
      firstDate: date,
      lastDate: date.add(Duration(days: 30)),
    );
  }
}
